package com.zg.nacos.web;

import cn.hutool.core.util.StrUtil;
import com.zg.common.core.R;
import com.zg.nacos.domain.req.PermissionEditReq;
import com.zg.nacos.domain.req.PermissionPageReq;
import com.zg.nacos.util.NacosUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriBuilder;
import reactor.core.publisher.Mono;

import javax.validation.Valid;
import java.time.Duration;
import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/20 16:11
 */

@RestController
@RequestMapping("/nacos")
@Api(tags = "nacos服务")
@Slf4j
public class NacosPermissionController {



    @Autowired
    private WebClient webClient;

    @ApiOperation(value="权限列表")
    @PostMapping("/permission/page")
    public Mono<R<Object>> pagePermission(@Valid  @RequestBody PermissionPageReq userPageReq) {
        return webClient.get()
                .uri(uriBuilder -> {
                    UriBuilder builder = uriBuilder.path("/nacos/v1/auth/permissions");
                    // 仅当参数非空时才添加 queryParam
                    if (!StrUtil.isEmpty(userPageReq.getRole())) {
                        builder.queryParam("role",userPageReq.getRole());
                    }
                    builder.queryParam("pageSize", userPageReq.getPageSize());
                    builder.queryParam("pageNo", userPageReq.getPageNo());
                    builder.queryParam("search", "accurate");
                    builder.queryParam("accessToken",userPageReq.getNacosToken());
                    return builder.build();
                })
                .retrieve()
                .bodyToMono(Map.class)
                .map(response -> NacosUtils.genPage(response, userPageReq.getPageNo(),userPageReq.getPageSize()));
    }

    @ApiOperation(value="权限新增")
    @PostMapping("/permission/add")
    public R addPermission(@Valid @RequestBody PermissionEditReq editReq) {
        MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();

        // 使用add方法添加表单字段（更适合multipart/form-data）
        formData.add("role", editReq.getRole());
        formData.add("resource", editReq.getResource());
        formData.add("action", editReq.getAction());

            Map map= webClient.post()
                    .uri(uriBuilder -> {
                        UriBuilder builder = uriBuilder.path("/nacos/v1/auth/permissions");
                        builder.queryParam("accessToken",editReq.getNacosToken());  // 仅当参数非空时才添加 queryParam
                        return builder.build();
                    })
                    .contentType(MediaType.MULTIPART_FORM_DATA)
                    .body(BodyInserters.fromMultipartData(formData))
                    .retrieve()
                    .bodyToMono(Map.class)
                    .block(Duration.ofSeconds(5));
            return NacosUtils.handleResponse(map);


    }
    @ApiOperation(value="权限删除")
    @PostMapping("/permission/remove")
    public R removePermission(@Valid @RequestBody PermissionEditReq editReq) {

            Map map= webClient.delete()
                    .uri(uriBuilder -> {
                        UriBuilder builder = uriBuilder.path("/nacos/v1/auth/permissions");
                        builder.queryParam("action",editReq.getAction());
                        builder.queryParam("role",editReq.getRole());
                        builder.queryParam("resource",editReq.getResource());
                        builder.queryParam("accessToken",editReq.getNacosToken());
                        // 仅当参数非空时才添加 queryParam
                        return builder.build();
                    })
                    .retrieve()
                    .bodyToMono(Map.class)
                    .block(Duration.ofSeconds(5));
            return NacosUtils.handleResponse(map);

    }



}